package ca.concordia.pga.main;

import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Set;

import ca.concordia.pga.models.Service;

public class Output {

	public void makeDirectory() {
		//create new folder for output
		File partitions = new File("output");		
		if (!partitions.exists())
			partitions.mkdir();
	}

	public void outputResults(ArrayList<Set<Service>> solution) {
		makeDirectory();
		try {
			File file = new File("output/solution.txt");
			file.createNewFile();
			Path path = Paths.get("output/solution.txt");
			BufferedWriter bw = Files.newBufferedWriter(path, Charset.forName(System.getProperty("file.encoding")), TRUNCATE_EXISTING);
			for (int i=0;i<solution.size();++i) {
				int j=0;
				for (Service s : solution.get(i)) {
					bw.write(s.getName());
					if (j!=solution.get(i).size()-1)
						bw.write(",");
					j++;
				}
				if (i!=solution.size()-1)
					bw.write(System.lineSeparator());
			}

			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
